package com.zrgj.manager.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.zrgj.domain.Order;
import com.zrgj.domain.OrderItem;
import com.zrgj.domain.ProductPo;
import com.zrgj.manager.mapper.OrderItemMapper;
import com.zrgj.manager.mapper.OrderMapper;
import com.zrgj.manager.mapper.ProductMapper;
import com.zrgj.manager.service.IOrderService;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

@Service
@Component
public class OrderServiceImpl implements IOrderService {
    private OrderMapper orderMapper;
    private OrderItemMapper orderItemMapper;
    private ProductMapper productMapper;

    public OrderServiceImpl(OrderMapper orderMapper, OrderItemMapper orderItemMapper, ProductMapper productMapper) {
        this.orderMapper = orderMapper;
        this.orderItemMapper = orderItemMapper;
        this.productMapper = productMapper;
    }

    @Override
    public List<Order> selectAll(Integer userId) {
        List<Order> orders = orderMapper.selectAll(userId);
        List<Order> orderList = new ArrayList<>();

        for (Order order: orders){
            List<OrderItem> orderItems = orderItemMapper.selectByOrderId(order.getOrderId());
            List<OrderItem> orderItemList = new ArrayList<>();
            for (OrderItem orderItem: orderItems){
                ProductPo productPo = productMapper.selectVo(orderItem.getProductId());
                orderItem.setProductPo(productPo);
                orderItemList.add(orderItem);
            }
            order.setOrderItems(orderItemList);
            orderList.add(order);
        }

        return orderList;
    }

    @Override
    public String insert(Order order) {
        String orderId = UUID.randomUUID().toString().replaceAll("-", "");
        order.setOrderId(orderId);
        orderMapper.insert(order);
        List<OrderItem> orderItems = order.getOrderItems();
        for (OrderItem orderItem: orderItems){
            orderItem.setOrderId(orderId);
            orderItemMapper.insert(orderItem);
        }
        return orderId;
    }

    @Override
    public void delete(String orderId) {
        orderMapper.delete(orderId);
        orderItemMapper.delete(orderId);
    }

    @Override
    public List<Order> select() {
        List<Order> orders = orderMapper.select();
        List<Order> orderList = new ArrayList<>();

        for (Order order: orders){
            List<OrderItem> orderItems = orderItemMapper.selectByOrderId(order.getOrderId());
            List<OrderItem> orderItemList = new ArrayList<>();
            for (OrderItem orderItem: orderItems){
                ProductPo productPo = productMapper.selectVo(orderItem.getProductId());
                orderItem.setProductPo(productPo);
                orderItemList.add(orderItem);
            }
            order.setOrderItems(orderItemList);
            orderList.add(order);
        }

        return orderList;
    }

    @Override
    public Order findById(String orderId) {
        Order order = orderMapper.findById(orderId);
        List<OrderItem> orderItems = orderItemMapper.selectByOrderId(order.getOrderId());
        List<OrderItem> orderItemList = new ArrayList<>();
        for (OrderItem orderItem: orderItems){
            ProductPo productPo = productMapper.selectVo(orderItem.getProductId());
            orderItem.setProductPo(productPo);
            orderItemList.add(orderItem);
        }
        order.setOrderItems(orderItemList);
        return order;
    }

    @Override
    public void update(Order order) {
        orderMapper.update(order);
    }
}
